
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       notify_tone_alarm.h
  * @author     baiyang
  * @date       2022-5-28
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include <rtthread.h>

#include "mml_player.h"
#include "notify_device.h"
/*-----------------------------------macro------------------------------------*/
// wait 2 seconds before assuming a tone is done and continuing the continuous tone
#define MB_NOTIFY_TONEALARM_MAX_TONE_LENGTH_MS 2000
#define MB_NOTIFY_TONEALARM_TONE_BUF_SIZE 100
/*----------------------------------typedef-----------------------------------*/
typedef struct tone_alarm* tone_alarm_t;

/// tonealarm_type - bitmask of states we track
struct tonealarm_type {
    uint16_t armed                 : 1;    // 0 = disarmed, 1 = armed
    uint16_t failsafe_battery      : 1;    // 1 if battery failsafe
    uint16_t parachute_release     : 1;    // 1 if parachute is being released
    uint16_t pre_arm_check         : 1;    // 0 = failing checks, 1 = passed
    uint16_t failsafe_radio        : 1;    // 1 if radio failsafe
    uint16_t failsafe_gcs          : 1;    // 1 if gcs failsafe
    uint16_t failsafe_ekf          : 1;    // 1 if ekf failsafe
    uint16_t vehicle_lost          : 1;    // 1 if lost copter tone requested
    uint16_t compass_cal_running   : 1;    // 1 if compass calibration is running
    uint16_t waiting_for_throw     : 1;    // 1 if waiting for copter throw launch
    uint16_t leak_detected         : 1;    // 1 if leak detected
    uint16_t powering_off          : 1;    // 1 if smart battery is powering off
    uint16_t temp_cal_running      : 1;    // 1 if temperature calibration is running
};

struct NotifyTone {
    const char *str;
    const uint8_t continuous : 1;
};

struct tone_alarm {
    // 
    struct notify_device notify_dev;

    struct tonealarm_type flags;
    bool _have_played_ready_tone : 1;

    int8_t _cont_tone_playing;
    int8_t _tone_playing;
    uint32_t _tone_beginning_ms;

    const struct NotifyTone* _tones;

    struct rt_mutex _mutex;
    struct mml_player _mml_player;
    char _tone_buf[MB_NOTIFY_TONEALARM_TONE_BUF_SIZE];
};

/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
notify_device_t tonealarm_probe();
/*------------------------------------test------------------------------------*/

#ifdef __cplusplus
}
#endif



